package com.cmcc.xdr.message;

import java.nio.ByteBuffer;

import com.cmcc.xdr.util.BufferLoadUtil;
import com.cmcc.xdr.util.TBCDUtil;


/**
 * 单接口信令公共信息
 * 
 * @author Administrator
 * 
 */
public class SingleSigGlobalMsg implements IXDRData {
	
	/**
	 * XDR数据类型：
	 *	1：合成XDR数据
	 *	2：单接口XDR数据
	 */
	private int xdrInfoType;
	
	/**
	 * unsigned int 2 <br>
	 * 全F 指示整个XDR所占用字节数
	 */
	private int totalLength;

	/**
	 * byte 2 全F <br>
	 * 城市区号，如010代表北京采用TBCD编码，<br>
	 * 每4个比特表示一个十进制数字
	 */
//	private String city;

	/**
	 * byte 2 全F <br>
	 * 当前省份,从PROVINCE字段扩展而来
	 */
	private String localProvince;  //新增  省份
	
	/**
	 * byte 2 全F <br>
	 * 当前地市,从City字段扩展而来
	 */
	private String localCity;
	
	/**
	 * byte 2 全F <br>
	 * 归属省份,从PROVINCE字段扩展而来
	 */
	private String ownerProvince;  //新增 归属省份
	
	/**
	 * byte 2 全F <br>
	 * 归属地市,从City字段扩展而来
	 */
	private String ownerCity;  //新增 归属城市
	
	/**
	 * byte 2 全F <br>
	 * 漫游类型
	 */
	private String roamingType;  //新增  漫游类型
	
	/**
	 * Interface unsigned int 1 全F <br>
	 * 接口类型：<br>
	 * 1. Uu 2. X2 3. UE_MR 4. Cell_MR 5. S1-MME <br>
	 * 6. S6a 7. S11 8. S10 9. SGs 10. S5/S8 <br>
	 * 11. S1-U 12. Gn-C 13. Gm 14. Mw 15. Mg <br>
	 * 16. ISC 17. Sv 18. Cx 19. Dx 20. Sh <br>
	 * 21. Dh 22. Gx23. Rx
	 */
	private Integer interfaceType;

	/**
	 * unsigned int 1 全F <br>
	 * RAT类型：<br>
	 * 1. UTRAN <br>
	 * 2. GERAN <br>
	 * 3. WLAN <br>
	 * 4. GAN <br>
	 * 5. HSPA Evolution<br>
	 * 6. EUTRAN
	 */
	private int rat;

	/**
	 * byte 16 全F <br>
	 * 数据合成服务器生成的唯一XDR编号，与采集解析服务器上报的单接口XDR ID相互独立； <br>
	 * 一个大流程生成一个XDR ID，关联流程内各接口上的XDR，因此一个合成XDR ID可对应同一UE在不同接口上的多个XDR记录。
	 */
	private String xdrID;

	/**
	 * unsigned int 1 全F <br>
	 * 1：合成信令XDR <br>
	 * 2：合成业务XDR <br>
	 * 3：合成MR XDR<br>
	 */
	private int xdrType;

	/**
	 * byte 8 全F <br>
	 * 用户IMSI（TBCD编码）
	 */
	private String imsi;
	
	/**
	 * byte 8 全F <br>
	 * 用户IMSI（TBCD编码）
	 */
	private String imeisv;

	/**
	 * byte 8 全F <br>
	 * 终端IMEI（TBCD编码）
	 */
	private String imei;

	/**
	 * byte 16 全F <br>
	 * 用户号码（TBCD编码）
	 */
	private String msisdn;
	
	/**
	 * 合成xdr通用字段
	 */
	private Integer procedureType;
	private long procedureStartTime;
	private long procedureEndTime;
	private Integer procedureStatus;
	private Integer xdrNumber;
	private Integer reasonCause183;
	private Integer serviceType;
	private String calledNumber;
	private String callingNumber;
    private Integer responseCode;
    private long firstFailTime;
    private String firstFailNeIp;
    private Integer finishReasOnCause;
	

	public SingleSigGlobalMsg(ByteBuffer buffer,String divisionXdrtype) {
		//首先校验是否有xdr数据类型拆分（XDRType）,如果有合成xdr数据或者拆分xdr数据才进行获取XDRType
		if("true".equals(divisionXdrtype)){//是否开启合成接口判断
			xdrInfoType = BufferLoadUtil.loadInt(buffer,1);
		}else{
			xdrInfoType = 2;
		}
		if(xdrInfoType == 2){//如果是单接口走下面
			totalLength = BufferLoadUtil.load(buffer,totalLength, 2);
//			log.info("=====totalLength====="+totalLength);
			localProvince = TBCDUtil.toTBCD(BufferLoadUtil.loadByte(buffer,2));
//			log.info("=====localProvince====="+localProvince);
			localCity = TBCDUtil.toTBCD(BufferLoadUtil.loadByte(buffer,2));
//			log.info("=====localCity====="+localCity);
			ownerProvince = TBCDUtil.toTBCD(BufferLoadUtil.loadByte(buffer,2));
//			log.info("=====ownerProvince====="+ownerProvince);
			ownerCity = TBCDUtil.toTBCD(BufferLoadUtil.loadByte(buffer,2));
//			log.info("=====ownerCity====="+ownerCity);
			roamingType = String.valueOf(BufferLoadUtil.loadInt(buffer,1));
//			log.info("=====roamingType====="+roamingType);
			interfaceType = BufferLoadUtil.load(buffer,interfaceType, 1);
//			log.info("=====interfaceType====="+interfaceType);
			xdrID = BufferLoadUtil.loadHexStr(buffer,16);
//			log.info("=====xdrID====="+xdrID);
			rat = BufferLoadUtil.load(buffer,rat, 1);
//			log.info("=====rat====="+rat);
			imsi = TBCDUtil.toTBCD(BufferLoadUtil.loadByte(buffer,8));
//			log.info("=====imsi====="+imsi);
			imei = TBCDUtil.toTBCD(BufferLoadUtil.loadByte(buffer,8));
//			log.info("=====imei====="+imei);
			msisdn = TBCDUtil.toTBCD(BufferLoadUtil.loadByte(buffer,16));
//			log.info("=====msisdn====="+msisdn);
		}else if(xdrInfoType == 1){//如果是合成接口走下面
			//公共信息
			totalLength = BufferLoadUtil.load(buffer,totalLength, 2);
//			log.info("=syn====totalLength====="+totalLength);
			localProvince = TBCDUtil.toTBCD(BufferLoadUtil.loadByte(buffer,2));
//			log.info("=syn====localProvince====="+localProvince);
			localCity = TBCDUtil.toTBCD(BufferLoadUtil.loadByte(buffer,2));
//			log.info("=syn====localCity====="+localCity);
			ownerProvince = TBCDUtil.toTBCD(BufferLoadUtil.loadByte(buffer,2));
//			log.info("=syn====ownerProvince====="+ownerProvince);
			ownerCity = TBCDUtil.toTBCD(BufferLoadUtil.loadByte(buffer,2));
//			log.info("=syn====ownerCity====="+ownerCity);
			roamingType = String.valueOf(BufferLoadUtil.loadInt(buffer,1));
//			log.info("=syn====roamingType====="+roamingType);
//			BufferLoadUtil.loadInt(1);
			rat = BufferLoadUtil.load(buffer,rat, 1);
//			log.info("=syn====rat====="+rat);
			xdrType = BufferLoadUtil.load(buffer,xdrType, 1);
//			log.info("=syn====xdrType====="+xdrType);
			xdrID = BufferLoadUtil.loadHexStr(buffer,16);
//			log.info("=syn====xdrID====="+xdrID);
			imsi = TBCDUtil.toTBCD(BufferLoadUtil.loadByte(buffer,8));
//			imei = TBCDUtil.toTBCD(BufferLoadUtil.loadByte(8));
			imeisv = TBCDUtil.toTBCD(BufferLoadUtil.loadByte(buffer,8));
			
//			log.info("=syn====imsi====="+imsi);
			
//			log.info("=syn====imei====="+imei);
			msisdn = TBCDUtil.toTBCD(BufferLoadUtil.loadByte(buffer,16));
//			log.info("=syn====msisdn====="+msisdn);
			
			//通用信息
			procedureType = BufferLoadUtil.loadInt(buffer,1);
			procedureStartTime = BufferLoadUtil.loadLong(buffer,8);
			procedureEndTime = BufferLoadUtil.loadLong(buffer,8);
			procedureStatus = BufferLoadUtil.loadInt(buffer,1);
			callingNumber = TBCDUtil.toTBCD(BufferLoadUtil.loadByte(buffer,16));
			calledNumber = TBCDUtil.toTBCD(BufferLoadUtil.loadByte(buffer,16));
//			serviceType = BufferLoadUtil.loadInt(1);
//			responseCode = BufferLoadUtil.loadInt(2);
//			reasonCause183 = BufferLoadUtil.loadInt(1);
//			finishReasOnCause = BufferLoadUtil.loadInt(2);
//			firstFailTime = BufferLoadUtil.loadLong(8);
//			firstFailNeIp = BufferLoadUtil.loadStr(16);
			xdrNumber = BufferLoadUtil.loadInt(buffer,1);
		}
	}

	public SingleSigGlobalMsg() {
	}

	/**
	 * 返回公共消息所占字节数
	 */
	public int getLength() {
		// 2+2+1+16+1+8+8+16=54
		return 54;
	}

//	public void encode(SDTPMessage message) {
//		message.write(totalLength, 2).write(localProvince, 2).write(localCity, 2).write(ownerProvince, 2).write(ownerCity, 2).write(roamingType, 2).write(interfaceType, 1).write(xdrID, 16).write(rat, 1).write(imsi, 8).write(imei, 8).write(msisdn, 16);
//	}

	public int getTotalLength() {
		return totalLength;
	}

	public void setTotalLength(int totalLength) {
		this.totalLength = totalLength;
	}

	public Integer getInterfaceType() {
		return interfaceType;
	}

	public void setInterfaceType(Integer interfaceType) {
		this.interfaceType = interfaceType;
	}

	public int getRat() {
		return rat;
	}

	public void setRat(int rat) {
		this.rat = rat;
	}

	public String getXdrID() {
		return xdrID;
	}

	public void setXdrID(String xdrID) {
		this.xdrID = xdrID;
	}

	public int getXdrType() {
		return xdrType;
	}

	public void setXdrType(int xdrType) {
		this.xdrType = xdrType;
	}

	public String getImsi() {
		return imsi;
	}

	public void setImsi(String imsi) {
		this.imsi = imsi;
	}

	public String getImei() {
		return imei;
	}

	public void setImei(String imei) {
		this.imei = imei;
	}

	public String getMsisdn() {
		return msisdn;
	}

	public void setMsisdn(String msisdn) {
		this.msisdn = msisdn;
	}

	public String getLocalProvince() {
		return localProvince;
	}

	public void setLocalProvince(String localProvince) {
		this.localProvince = localProvince;
	}

	public String getLocalCity() {
		return localCity;
	}

	public void setLocalCity(String localCity) {
		this.localCity = localCity;
	}

	public String getOwnerProvince() {
		return ownerProvince;
	}

	public void setOwnerProvince(String ownerProvince) {
		this.ownerProvince = ownerProvince;
	}

	public String getOwnerCity() {
		return ownerCity;
	}

	public void setOwnerCity(String ownerCity) {
		this.ownerCity = ownerCity;
	}

	public String getRoamingType() {
		return roamingType;
	}

	public void setRoamingType(String roamingType) {
		this.roamingType = roamingType;
	}

	public int getXdrInfoType() {
		return xdrInfoType;
	}

	public void setXdrInfoType(int xdrInfoType) {
		this.xdrInfoType = xdrInfoType;
	}

	public Integer getProcedureStatus() {
		return procedureStatus;
	}

	public void setProcedureStatus(Integer procedureStatus) {
		this.procedureStatus = procedureStatus;
	}

	public Integer getXdrNumber() {
		return xdrNumber;
	}

	public void setXdrNumber(Integer xdrNumber) {
		this.xdrNumber = xdrNumber;
	}

	public String getImeisv() {
		return imeisv;
	}

	public void setImeisv(String imeisv) {
		this.imeisv = imeisv;
	}

	public long getProcedureStartTime() {
		return procedureStartTime;
	}

	public void setProcedureStartTime(long procedureStartTime) {
		this.procedureStartTime = procedureStartTime;
	}

	public long getProcedureEndTime() {
		return procedureEndTime;
	}

	public void setProcedureEndTime(long procedureEndTime) {
		this.procedureEndTime = procedureEndTime;
	}

	public Integer getReasonCause183() {
		return reasonCause183;
	}

	public void setReasonCause183(Integer reasonCause183) {
		this.reasonCause183 = reasonCause183;
	}

	public Integer getServiceType() {
		return serviceType;
	}

	public void setServiceType(Integer serviceType) {
		this.serviceType = serviceType;
	}

	public String getCalledNumber() {
		return calledNumber;
	}

	public void setCalledNumber(String calledNumber) {
		this.calledNumber = calledNumber;
	}

	public String getCallingNumber() {
		return callingNumber;
	}

	public void setCallingNumber(String callingNumber) {
		this.callingNumber = callingNumber;
	}

	public Integer getResponseCode() {
		return responseCode;
	}

	public void setResponseCode(Integer responseCode) {
		this.responseCode = responseCode;
	}

	public long getFirstFailTime() {
		return firstFailTime;
	}

	public void setFirstFailTime(long firstFailTime) {
		this.firstFailTime = firstFailTime;
	}

	public String getFirstFailNeIp() {
		return firstFailNeIp;
	}

	public void setFirstFailNeIp(String firstFailNeIp) {
		this.firstFailNeIp = firstFailNeIp;
	}

	public Integer getProcedureType() {
		return procedureType;
	}

	public void setProcedureType(Integer procedureType) {
		this.procedureType = procedureType;
	}

	public Integer getFinishReasOnCause() {
		return finishReasOnCause;
	}

	public void setFinishReasOnCause(Integer finishReasOnCause) {
		this.finishReasOnCause = finishReasOnCause;
	}
	
	
	

}